

package test.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;


public class DB_Builder {

    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;

    @Override
    protected void finalize() {

	try {
	    disconnect();
	    super.finalize();
	} catch (Throwable e) {
	    e.printStackTrace();
	}
    }

    public void disconnect() {
	try {
	    if (rs != null)
		rs.close();
	    if (stmt != null)
		stmt.close();
	    if (conn != null)
		conn.close();
	} catch (SQLException se) {
	    System.out.println("SQL Exception:");
	    while (se != null) {
		System.out.println("State  : " + se.getSQLState());
		System.out.println("Message: " + se.getMessage());
		System.out.println("Error  : " + se.getErrorCode());
		se = se.getNextException();
	    }
	}
    }

    public void connect() {

	try {
	    // Load the database driver
	    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

	    String url = "";
	    conn = DriverManager.getConnection(url, "sseo_ro", "sseo_ro");

	    for (SQLWarning warn = conn.getWarnings(); warn != null; warn = warn
	    .getNextWarning()) {
		System.out.println("SQL Warning:");
		System.out.println("State  : " + warn.getSQLState());
		System.out.println("Message: " + warn.getMessage());
		System.out.println("Error  : " + warn.getErrorCode());
	    }
	} catch (SQLException se) {
	    // Loop through the SQL Exceptions
	    System.out.println("SQL Exception:");
	    while (se != null) {
		System.out.println("State  : " + se.getSQLState());
		System.out.println("Message: " + se.getMessage());
		System.out.println("Error  : " + se.getErrorCode());
		se = se.getNextException();
	    }
	} catch (Exception e) {
	    System.out.println(e);
	}
    }

    public ResultSet execute(String query) {
	ResultSet rs1 = null;

	try {

	    // Get a statement from the connection and execute the query
	    stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
		    ResultSet.CONCUR_READ_ONLY);

	    rs1 = stmt.executeQuery(query);

	} catch (SQLException se) {
	    System.out.println("SQL Exception:");

	    // Loop through the SQL Exceptions
	    while (se != null) {
		System.out.println("State  : " + se.getSQLState());
		System.out.println("Message: " + se.getMessage());
		System.out.println("Error  : " + se.getErrorCode());
		se = se.getNextException();
	    }
	} catch (Exception e) {
	    System.out.println(e);
	}
	return rs1;
    }

}
